package com.example.myapplication.ui;

import android.os.Bundle;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.lifecycle.ViewModelProvider;

import com.example.myapplication.R;
import com.example.myapplication.databinding.ActivityAddNoteBinding;
import com.example.myapplication.entity.Note;

public class AddNoteActivity extends AppCompatActivity {
    private ActivityAddNoteBinding binding;
    private NoteViewModel noteViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 1. 使用 ViewBinding 绑定布局
        binding = ActivityAddNoteBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        // 2. 获取 ViewModel
        noteViewModel = new ViewModelProvider(this).get(NoteViewModel.class);
        // 3. 按钮点击事件
        binding.buttonSave.setOnClickListener(v -> {
            String title = binding.etTitle.getText().toString().trim();
            String content = binding.etContent.getText().toString().trim();

            if (title.isEmpty() || content.isEmpty()) {
                Toast.makeText(this, "标题和内容不能为空", Toast.LENGTH_SHORT).show();
            } else {
                Note newNote = new Note(title, content);
                noteViewModel.insert(newNote);
                Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
                finish(); // 保存后关闭页面
            }
        });

    }
}